//
//  MyFavoriteViewController.m
//  EverydayDemo
//
//  Created by qianfeng01 on 15-7-22.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "MyFavoriteViewController.h"
#import "HomePageModel.h"
#import "DBManager.h"
#import "DetailViewController.h"
@interface MyFavoriteViewController ()
@property(nonatomic,strong)NSMutableArray *removeArray;
@property(nonatomic,strong)NSArray *arr;
@property(nonatomic,strong)UIButton *deleteButton;
@end

@implementation MyFavoriteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showUI];
    if (self.arr.count<1) {
        self.title=@"您还没有收藏";
    }else
    {
        [self initUI];
        
    }
    
    
}
- (NSMutableArray *)removeArray {
    if (_removeArray == nil) {
        _removeArray = [[NSMutableArray alloc] init];
    }
    return _removeArray;
}
- (void)initUI {
    UIButton *editButton = [UIButton buttonWithType:UIButtonTypeSystem];
    editButton.tintColor = [UIColor clearColor];
    editButton.frame = CGRectMake(0, 0, 45, 35);
    [editButton setTitle:@"编辑" forState:UIControlStateNormal];
    [editButton setTitle:@"完成" forState:UIControlStateSelected];
    [editButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [editButton setTitleColor:[UIColor purpleColor] forState:UIControlStateSelected];
    [editButton addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:editButton];
    self.navigationItem.rightBarButtonItem = item;
    
    // 删除按钮
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)];
    view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.deleteButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_deleteButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    [_deleteButton setBackgroundColor:[UIColor whiteColor]];
    _deleteButton.frame = CGRectMake(0, 10, view.frame.size.width, 40);
    [_deleteButton addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:_deleteButton];
    if (!self.tableView.isEditing) {
        _deleteButton.hidden=YES;
    }
    [self.tableView setTableFooterView:view];
}
- (void)delete:(id)sender {
    [self.dataArr removeObjectsInArray:self.removeArray];
    [self.tableView reloadData];
    for (HomePageModel *model in self.removeArray) {
        [[DBManager sharedManager]deleteModelForAppId:model.articleId];
    }
    
}

- (void)edit:(UIButton *)button {
    button.selected = !button.selected;
    if (button.selected==YES) {
        _deleteButton.hidden=NO;
    }else if (button.selected==NO){
        _deleteButton.hidden=YES;
    }
    [self.tableView setEditing:button.selected animated:YES];
}

-(void)showUI
{
    
    self.arr=[[DBManager sharedManager]readModels];
    for (HomePageModel *model in self.arr) {
        [self.dataArr addObject:model];
    }
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete |
    UITableViewCellEditingStyleInsert;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.tableView.isEditing) {
        HomePageModel *model=self.dataArr[indexPath.row];
        [self.removeArray addObject:model];
    }else{
        DetailViewController *cnv=[[DetailViewController alloc]init];
        HomePageModel *model=self.dataArr[indexPath.row];
        cnv.url=model.url;
        cnv.articleId=model.articleId;
        cnv.imageUrl=model.imageUrl;
        cnv.Artitle=model.title;
        cnv.categryName=model.categryName;
        cnv.authorName=model.authorName;
        cnv.readCount=model.readCount;
        cnv.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:cnv animated:YES];
    }
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.tableView.isEditing) {
        HomePageModel *model=self.dataArr[indexPath.row];
        [self.removeArray removeObject:model];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
